package com.cmict.data.bpm.framework.bpm.core.event;

import com.cmict.data.bpm.dal.dataobject.task.BpmProcessInstanceExtDO;
import com.cmict.data.bpm.dal.mysql.task.BpmProcessInstanceExtMapper;
import com.cmict.data.bpm.enums.definition.BpmProcessDefineEnum;

import com.cmict.data.bpm.enums.task.BpmProcessInstanceResultEnum;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.util.Map;

/**
 * 资源申请审批通过回调
 *
 * @author liutao
 * @date 2022/11/10
 */

@Component
public class BpmProcessInstanceResourceEventListener extends BpmProcessInstanceResultEventListener {

    @Value("${callback.url.exchange.zysq:}")
    private String ZYSQ_BACK_URL;
    @Resource
    private BpmProcessInstanceExtMapper processInstanceExtMapper;

    @Override
    protected String getProcessDefinitionKey() {
        return BpmProcessDefineEnum.ZYSQ.name().toLowerCase();
    }

    @Override
    protected void onEvent(BpmProcessInstanceResultEvent event) {
        Integer result = event.getResult();
        if (result == BpmProcessInstanceResultEnum.APPROVE.getResult()) {
            BpmProcessInstanceExtDO processInstanceExt = processInstanceExtMapper.selectByProcessInstanceId(event.getId());
            Map<String, Object> formVariables = processInstanceExt.getFormVariables();
            RestTemplate restTemplate = new RestTemplate();
            restTemplate.postForEntity(ZYSQ_BACK_URL, formVariables, String.class);
        }
    }

}
